<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="public/css/bootstrap.min.css">
    <script src="public/js/jquery-3.4.1.min.js"></script>
    <script src="public/js/socket.io.js"></script>
    <title>Document</title>
    <script>
        $(function() {
            var socket = io('/', {
                transport: [ 'websocket' ]
            });
            socket.on('returnMsg', (data) => {
                $('#messageList').append(`<div><strong>${data.username}</strong>:${data.msg}</div>`)
            })
            window.socket = socket;
        })
        function send() {
            const msg = $('#message').val();
            if(msg === ''){
                alert('发送消息不能为空！');
            } else {
                window.socket.emit('sendMsg', { msg });
                $('#message').val('');
            }
        }
    </script>
</head>
<body>
    <div class="row">
        <p>
            hi <%=username%>
        </p>
    </div>
    <div class="row">
        <h1>
            聊天室
        </h1>
    </div>
    <div class="row">
        <div class="chat" id="messageList">

        </div>
    </div>
    <div class="row">
        <textarea id="message">

        </textarea>
        <input type="button" value="发送" onclick="send()" class="btn btn-primary">
    </div>
</body>
</html>